package com.linzelin.controller;

import com.linzelin.dao.EmployeeDao;
import com.linzelin.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Collection;

@Controller
public class UserController {

    @RequestMapping("/user/login")
    public String login(HttpSession session, @RequestParam("username") String username , @RequestParam("password")String password, Model model){
        if(!StringUtils.isEmpty(username)&&"123456".equals(password)){
            session.setAttribute("user",username);
            return "redirect:/login";
        }
        else{
            model.addAttribute("msg","用户名或密码错误");
            return "index";
        }
    }

    @Autowired
    private EmployeeDao employeeDao;

    @RequestMapping("/getAllEmploy")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        System.out.println("进入了");
        return "main";
    }

    @GetMapping("/add")
    @ResponseBody
    public String getEmp(){
        return "get";
    }

    @PostMapping("/add")
    @ResponseBody
    public String addEmp(){
        return "post";
    }

    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();
        //清除所有的session数据
        return "index";
    }
}
